% --- indexing.tex ---

% INDEXING MACROS to be used in conjunction with makeindex.

% The notion is to say
% 	\input index
% at the top of your source file, then to insert into your source text
% references according to the following examples:

% --- EXAMPLES ---
% ^{subject} (Roman type.)
% ^[topic] (Boldfaced.)
% ^(start( ... ^)start) (Boldfaced page range.)
% ^:definition:  (Underlined.)
% ^<example> (Italics.)
%
% All the above will also be printed as well as indexed. If you don't want
% to print, use ^^ instead of ^.

% When you TeX your source, a file source.idx will be created. Run
% makeindex on that file. That gives you a file source.ind that can be
% processed with LaTeX. Say
%	latex -s mx.sty -p any source.idx
% The style file mx.sty defines the appropriate macros, in addition to those
% supplied by LaTeX.

\newtoks\xarg

{\catcode`\{=12 \catcode`\}=12
\catcode`\[=1 \catcode`\]=2
\catcode`\/=0
\catcode`\\=12
/gdef/indexo#1#2[/xarg[#1]/def/next[#2]/ifx/next/empty/else/def/next[|#2]/fi
%/Marginalxref[#1]% Intended to make a remark in the margin in the early
% phases of composing; not defined here.
/edef/writeout[/write/indexfile[\indexentry{/the/xarg/next}%
{/noexpand/the/noexpand/Pageno}]]/writeout]
%/immediate/write/indexfile[\indexentry{#1/next}{/the/Pageno}]]
]

\def\index#1{\indexo{#1}{}}		\def\Index#1{#1\index{#1}}
\def\wholetopic#1{\indexo{#1}{bold}}
\def\starttopic#1{\indexo{#1}{(bold}}
\def\endtopic#1{\indexo{#1}{)}}
\def\defn#1{\indexo{#1}{undertext}}	\def\Defn#1{#1\defn{#1}}
\def\example#1{\indexo{#1}{italic}}	\def\Example#1{#1\example{#1}}

% Silent entries (cross-referenced, but not printed) are signified by ^^
% instead of ^.
\newif\ifsilent
\def\specialhat{\ifmmode\def\next{^}\else\let\next=\beginxref\fi\next}
\def\beginxref{\futurelet\next\beginxrefswitch}
\def\beginxrefswitch{\ifx\next\specialhat\let\next=\silentxref
		\else\silentfalse\let\next=\xref\fi
	\next}
\catcode`\^=\active \let^=\specialhat
\def\silentxref^{\silenttrue\xref}

\def\xref{\futurelet\next\xrefswitch}

\def\xrefswitch{\begingroup
	\ifx\next[\aftergroup\bracketxref
	\else\ifx\next:\aftergroup\colonxref
	\else\ifx\next<\aftergroup\anglexref
	\else\ifx\next(\aftergroup\lparxref
	\else\ifx\next)\aftergroup\rparxref
	\else\aftergroup\normalxref
	\fi\fi\fi\fi\fi
	\endgroup}

\def\xxref#1#2{\begingroup\ifsilent\aftergroup\ignorespaces
	\else#2\fi#1{#2}\endgroup}
\def\bracketxref[#1]{\xxref\wholetopic{#1}}
\def\lparxref(#1({\xxref\starttopic{#1}}
\def\rparxref)#1){\xxref\endtopic{#1}}
\def\colonxref:#1:{\xxref\defn{#1}}
\def\anglexref<#1>{\xxref\example{#1}}
\def\normalxref#1{\xxref\index{#1}}

\newwrite\indexfile
\immediate\openout\indexfile=\jobname.idx
